package com.keyinong.jishibao.adapter;

import java.util.ArrayList;

import com.keyinong.jishibao.R;
import com.keyinong.jishibao.bean.GoodsSortBean;
import com.tiyinong.util.CircleImageView;
import com.tiyinong.util.ImageUtils;

import android.content.Context;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class GoodsSortAdapter extends BaseAdapter {
	Context context;
	ArrayList<GoodsSortBean> list;
	int colorStr[] = { R.color.goodsSort1, R.color.goodsSort2,
			R.color.goodsSort3, R.color.goodsSort4, R.color.goodsSort5 };

	public GoodsSortAdapter(Context context, ArrayList<GoodsSortBean> list) {
		super();
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		int colorIndex;
		int width = windowWidth();
		ViewHandle handle;
		if (view == null) {
			handle = new ViewHandle();
			view = LayoutInflater.from(context).inflate(
					R.layout.item_home_goodssort, null);
			handle.rl_itemBigSort = (RelativeLayout) view
					.findViewById(R.id.rl_itemBigSort);
			handle.tv_goodsSort = (TextView) view
					.findViewById(R.id.tv_goodsSort);
			handle.tv_sortPresent = (TextView) view
					.findViewById(R.id.tv_sortPresent);
			handle.img_goodsSort = (CircleImageView) view
					.findViewById(R.id.img_goodsSort);
			view.setTag(handle);
		} else {
			handle = (ViewHandle) view.getTag();
		}
		handle.tv_goodsSort.setText(list.get(position).getName());
		colorIndex = position;
		if (position >= colorStr.length) {
			colorIndex = (position % colorIndex);
		}

		handle.tv_goodsSort.setTextColor(context.getResources().getColor(
				colorStr[colorIndex]));
		handle.tv_sortPresent.setText(list.get(position).getDescription());
		 if (width < 1080) {
		 ImageUtils.displayImage(context, list.get(position).getPic2(),
		 handle.img_goodsSort, R.drawable.ic_launcher);
		 } else {
		 ImageUtils.displayImage(context, list.get(position).getPic3(),
		 handle.img_goodsSort, R.drawable.ic_launcher);
		 }
		return view;
	}

	private class ViewHandle {
		TextView tv_goodsSort;
		TextView tv_sortPresent;
		 CircleImageView img_goodsSort;
//		ImageView img_goodsSort;
		RelativeLayout rl_itemBigSort;
	}

	private int windowWidth() {
		WindowManager wManager = (WindowManager) (context
				.getSystemService(Context.WINDOW_SERVICE));
		DisplayMetrics dMetrics = new DisplayMetrics();
		wManager.getDefaultDisplay().getMetrics(dMetrics);
		int width = dMetrics.widthPixels;
		return width;
	}

}
